//
//  NetTool.m
//  YiLiao
//
//  Created by mac on 16/7/6.
//  Copyright © 2016年 Noah. All rights reserved.
//

#import "NetTool.h"

static MBProgressHUD *HUD=nil;

@implementation NetTool

+(AFHTTPSessionManager *)getSessionManager{
    
    if ([self checkNetwoork]==NO) {
        [Util showAlertWithMessager:@"网络错误,请稍后重试" confirmBlock:nil];
        return nil;
    }
    [self showlndicator];
    AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
    mgr.requestSerializer=[AFHTTPRequestSerializer serializer];
    mgr.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    return mgr;
}
//显示
+(void)showlndicator{
    if (!HUD) {
        HUD=[MBProgressHUD showHUDAddedTo:WINDOW animated:YES];
    }
    [HUD show:YES];
    
    
}
//隐藏
+(void)hidelndicator{
    if (HUD) {
        [HUD hide:YES];
    }
}
//监听网络
+(void)startMonitoring{
    AFNetworkReachabilityManager *mgr=[AFNetworkReachabilityManager sharedManager];
    //监听网络
    [mgr startMonitoring];
    
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        //status状态
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"网络状态未知");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"没有可联网");
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"3G网络");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"WIFI");
                break;
            default:
                break;
        }
    }];
    
    
}
//要想获取到当前的网络状态，必须先开启网络监听状态，而网络监听又需要消耗一定的时间来执行，所以，一般情况下，我们监听网络状态都是在Appdelegate里面去开启监听的
+(BOOL)checkNetwoork{
    AFNetworkReachabilityManager *mgr=[AFNetworkReachabilityManager sharedManager];
    
    return mgr.reachable;
}
@end
